package com.huangpuweb.task.controller;

import com.huangpuweb.task.Constants;
import com.huangpuweb.task.model.HpSubwayExitInfo;
import com.huangpuweb.task.model.QZDwdDzGjDitieJczrsDi;
import com.huangpuweb.task.service.SyncSysDataService;
import com.huangpuweb.task.service.SyncZYJJZDataService;
import com.huangpuweb.task.util.DateUtil;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 定时同步地铁站数据
 */
@Controller
public class SyncSubwayDataJob {

    private static Logger logger = Logger.getLogger(SyncSubwayDataJob.class);


    private SyncSysDataService syncSysDataService;

    @Autowired
    public void setSyncSysDataService(SyncSysDataService syncSysDataService) {
        this.syncSysDataService = syncSysDataService;
    }

    private SyncZYJJZDataService syncZYJJZDataService;

    @Autowired
    public void setSyncZYJJZDataService(SyncZYJJZDataService syncZYJJZDataService) {
        this.syncZYJJZDataService = syncZYJJZDataService;
    }


    public void SyncSubwayDataJobTask() throws Exception {

        LogFactory.getLog(this.getClass()).info("--------start-------SyncSubwayDataJobTask----------" + new Date());

        /**
         * 同步各个景区路况信息到表hp_road_condition
         */
        String[] DITIECodeArr = Constants.JINGQU_DITIE.split(",");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String lastday = DateUtil.getYesterday() + " 00:00:00";
        QZDwdDzGjDitieJczrsDi listQZDwdDzGjDitieJczrsDi =null;
        for (int i = 0; i < DITIECodeArr.length; i++) {
            String[] ditieAndSpotCode = DITIECodeArr[i].split("-");
            listQZDwdDzGjDitieJczrsDi= syncZYJJZDataService.getListQZDwdDzGjDitieJczrsDi(ditieAndSpotCode[0], lastday);
            if (listQZDwdDzGjDitieJczrsDi != null) {


                HpSubwayExitInfo subwayExitInfo = new HpSubwayExitInfo();
                subwayExitInfo.setSubwayId(listQZDwdDzGjDitieJczrsDi.getZdid());
                subwayExitInfo.setInTotalNum((int)Double.parseDouble(listQZDwdDzGjDitieJczrsDi.getJzrs()));
                subwayExitInfo.setOutTotalNum((int)Double.parseDouble(listQZDwdDzGjDitieJczrsDi.getCzrs()));
                subwayExitInfo.setSpotCode(ditieAndSpotCode[1]);
                subwayExitInfo.setZdmc(listQZDwdDzGjDitieJczrsDi.getZdmc());
                subwayExitInfo.setCreate_time(lastday);
                syncSysDataService.insertOrUpdatesubwayExitInfo(subwayExitInfo);


            } else {
                lastday = DateUtil.getLastYesterday() + " 00:00:00";
                listQZDwdDzGjDitieJczrsDi = syncZYJJZDataService.getListQZDwdDzGjDitieJczrsDi(ditieAndSpotCode[0], lastday);
                HpSubwayExitInfo subwayExitInfo = new HpSubwayExitInfo();
                subwayExitInfo.setSubwayId(listQZDwdDzGjDitieJczrsDi.getZdid());
                subwayExitInfo.setInTotalNum(Integer.parseInt(listQZDwdDzGjDitieJczrsDi.getJzrs()));
                subwayExitInfo.setOutTotalNum(Integer.parseInt(listQZDwdDzGjDitieJczrsDi.getCzrs()));
                subwayExitInfo.setSpotCode(ditieAndSpotCode[1]);
                subwayExitInfo.setZdmc(listQZDwdDzGjDitieJczrsDi.getZdmc());
                subwayExitInfo.setCreate_time(lastday);
                syncSysDataService.insertOrUpdatesubwayExitInfo(subwayExitInfo);
            }







            ////            for(QZDwdDzGjDitieJczrsDi qz:listQZDwdDzGjDitieJczrsDi){
////
////                if (null!=qz.getZdmc()) {
////
////                    HpSubway hpSubway=new HpSubway();
////
////
////                    //设置地铁线路
////                    hpSubway.setSubwayLine(qz.getId());
////                    //设置地铁名称
////                    hpSubway.setSubwayName(qz.getZdmc());
////                    //设置地铁编号
////                    hpSubway.setSubwayId(qz.getZdid());
////                    //设置景区编码
////                    hpSubway.setSpotCode(ditieAndSpotCode[1]);
////
////                    hpSubway.setGd_jd(qz.getGd_jd()+"");
////                    hpSubway.setGd_wd(qz.getGd_wd()+"");
////                    hpSubway.setId(UUID.randomUUID().toString().replaceAll("-", ""));
////                    boolean flag=syncSysDataService.checkSubwayData(qz.getZdid(),ditieAndSpotCode[1]);
//////                    if (flag) {
//////                        //log.info("已经存在该条地铁站数据，进行更新操作");
//////                        //dao().update(hp_subway);
//////                        syncSysDataService.updateHpSubway(hpSubway);
//////                    }else{
//////                        //log.info("不存在该条地铁站数据，新增到数据库");
//////                        //dao().insert(hp_subway);
//////                        syncSysDataService.insertHpSubway(hpSubway);
//////                    }
////                    //log.info("同步该条地铁站数据成功");
////
////                    if (!flag) {
////                        //log.info("已经存在该条地铁站数据，进行更新操作");
////                        //dao().update(hp_subway);
////                        syncSysDataService.insertHpSubway(hpSubway);
////                    }
////                    List<QZDwdDzGjDitieJczrsDi> listHpSubwayExitInfo=syncZYJJZDataService.getListHpSubwayExitInfo(qz.getZdid(),lastday);
////
////
////                    for (QZDwdDzGjDitieJczrsDi subwayInfo : listHpSubwayExitInfo) {
////                        HpSubwayExitInfo subwayExitInfo = new HpSubwayExitInfo();
////                        subwayExitInfo.setId(subwayInfo.getDwd_zjid());
////                        subwayExitInfo.setSubwayId(subwayInfo.getZdid());
////                        subwayExitInfo.setEnter_number(Integer.valueOf(subwayInfo.getJzrs()));
////                        subwayExitInfo.setExit_number(Integer.valueOf(subwayInfo.getCzrs()));
////                        subwayExitInfo.setStartTime(sdf.format(subwayInfo.getKssj()));
////                        subwayExitInfo.setEndTime(sdf.format(subwayInfo.getJssj()));
////                        subwayExitInfo.setRecordTime(sdf.format(subwayInfo.getJhpt_update_time()));
////                        subwayExitInfo.setUpdateTime(DateUtil.getDateTime());
////                        syncSysDataService.insertOrUpdatesubwayExitInfo(subwayExitInfo);
////                        //log.info("同步地铁站详细信息数据成功");
////                    }
////
////                }
//            }
        }


        LogFactory.getLog(this.getClass()).info("--------end-------SyncSubwayDataJobTask----------" + new Date());

    }


}
